package com.graduation.mybatis.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.graduation.mybatis.bean.*;
import com.graduation.mybatis.mapper.DietPlansMapper;
import com.graduation.mybatis.mapper.PlanFoodsMapper;
import com.graduation.mybatis.mapper.RecommendationFoodsMapper;
import com.graduation.mybatis.service.CoachRecommendationsService;
import com.graduation.mybatis.mapper.CoachRecommendationsMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author 23726
* @description 针对表【coach_recommendations】的数据库操作Service实现
* @createDate 2025-04-22 12:00:12
*/
@Service
public class CoachRecommendationsServiceImpl extends ServiceImpl<CoachRecommendationsMapper, CoachRecommendations>
    implements CoachRecommendationsService{

    @Resource
    private CoachRecommendationsMapper coachRecommendationsMapper;
    @Resource
    private RecommendationFoodsMapper recommendationFoodsMapper;
    @Resource
    private DietPlansMapper dietPlansMapper;
    @Resource
    private PlanFoodsMapper planFoodsMapper;

    public List<CoachRecommendations> getAllCoachRecommendations(){
        // 1. 获取所有教练推荐基本信息
        List<CoachRecommendations> recommendations = coachRecommendationsMapper.selectAllRecommendations();

        // 2. 为每个推荐加载餐次和食物信息
        recommendations.forEach(recommendation -> {
            // 获取推荐的所有食物
            List<RecommendationFoodsVO> foodVOs = recommendationFoodsMapper
                    .selectFoodDetailsByRecommendationId(recommendation.getRecommendationId());

            // 按餐次分组
            Map<Integer, List<RecommendationFoodsVO>> foodsByMeal = foodVOs.stream()
                    .collect(Collectors.groupingBy(RecommendationFoodsVO::getMealType));

            // 构建餐次列表
            List<RecommendationMeal> meals = foodsByMeal.entrySet().stream()
                    .map(entry -> {
                        String mealName = getMealName(entry.getKey());
                        return new RecommendationMeal(entry.getKey(), mealName, entry.getValue());
                    })
                    .collect(Collectors.toList());

            recommendation.setMeals(meals);
        });

        return recommendations;
    }


    public void applyCoachRecommendation(Integer userId, Long recommendationId) {
        // 1. 获取推荐计划的所有食物
        List<RecommendationFoods> recommendationFoods =
                recommendationFoodsMapper.selectByRecommendationId(recommendationId);

        if (recommendationFoods.isEmpty()) {
            return ;
        }

        // 2. 获取或创建用户饮食计划
        DietPlans currentPlan = dietPlansMapper.selectActivePlanByUserId(userId);
        Integer planId;

        if (currentPlan == null) {
            // 创建新计划
            DietPlans newPlan = new DietPlans();
            newPlan.setUserId(userId);
            newPlan.setPlanName("教练推荐计划");
            newPlan.setIsActive(1);
            dietPlansMapper.insertNewPlan(newPlan);
            planId = newPlan.getPlanId();
        } else {
            // 使用现有计划
            planId = currentPlan.getPlanId();
            // 清空现有食物
            planFoodsMapper.deleteByPlanId(planId);
        }

        // 3. 转换推荐食物为用户计划食物
        List<PlanFoods> planFoods = recommendationFoods.stream()
                .map(rf -> new PlanFoods(
                        planId,
                        rf.getFoodId(),
                        rf.getMealType(),
                        rf.getGrams()
                ))
                .collect(Collectors.toList());

        // 4. 批量插入食物到用户计划
        if (!planFoods.isEmpty()) {
            planFoodsMapper.batchInsertFoods(planId, planFoods);
        }
    }

    private String getMealName(Integer mealType) {
        switch (mealType) {
            case 1: return "早餐";
            case 2: return "午餐";
            case 3: return "晚餐";
            case 4: return "加餐";
            default: return "其他";
        }
    }
}




